#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using std::abs;
using std::vector;
using std::cin;
using std::cout;
using std::set;
using std::pair;
using std::make_pair;
using std::min;

const int INF = 1000000001;

class Field {
    struct Point {
        int x, y, weight;
        Point() : x(0), y(0), weight(0){}
        Point(int new_x, int new_y, int new_weight) :
                x(new_x), y(new_y), weight(new_weight) {}
        bool operator<(Point other) const {
            if (x == other.x) {
                return y < other.y;
            }
            return x < other.x;
        }
        bool operator==(Point other) const {
            return weight == other.weight && x == other.x && y == other.y;
        }
        bool operator!=(Point other) const {
            return weight != other.weight || x != other.x || y != other.y;
        }
    };

    int height;
    int width;
    vector < vector <Point> > field;

    void make_neighbour_list(Point &point, vector <Point*> &list) {
        int shift_x[4] = {-1, 0, 1, 0};
        int shift_y[4] = {0, 1, 0, -1};
        for (int i = 0; i < 4; ++i) {
            if (point.x + shift_x[i] >= 0
                    && point.x + shift_x[i] < height
                    && point.y + shift_y[i] >= 0
                    && point.y + shift_y[i] < width) {
                list.push_back(&field[point.x + shift_x[i]][point.y + shift_y[i]]);
            }
        }
    }

public:
    Field(int new_height, int new_weight) {
        height = new_height;
        width = new_weight;
        vector <Point> line(width);
        field.assign(height, line);
        for (int line = 0; line != height; ++line) {
            for (int col = 0; col != width; ++col) {
                int weight;
                cin >> weight;
                field[line][col] = Point(line, col, weight);
            }
        }
    }

    vector<Point> operator[](int &line) {
        return field[line];
    }

    // Algorithm - A*
    int find_step_count(int begin_x, int begin_y, int end_x, int end_y) {
        Point begin = field[begin_x - 1][begin_y - 1];
        Point end = field[end_x - 1][end_y - 1];
        vector < vector <int> > step_count;
        vector <int> line(width, INF);
        step_count.assign(height, line);
        step_count[begin.x][begin.y] = 0;

        set < pair <int, Point*> > reachable_points;
        reachable_points.insert(make_pair (abs(begin.x - end.x) + abs(begin.y - end.y), &begin));

        while (!reachable_points.empty()) {
            bool finded = 0;
            Point current = *(reachable_points.begin()->second);
            reachable_points.erase(reachable_points.begin());
            vector <Point*> neighbours;
            make_neighbour_list(current, neighbours);
            for (auto it : neighbours) {
                if (it->weight == 0 && step_count[it->x][it->y] > step_count[current.x][current.y] + 1) {
                    step_count[it->x][it->y] = step_count[current.x][current.y] + 1;
                    reachable_points.insert(make_pair(step_count[it->x][it->y]
                            + abs(it->x - end.x) + abs(it->y - end.y), &(*it)));
                    if (*it == end) {
                        finded = 1;
                        break;
                    }
                }
            }
            if (finded == 1) {
                break;
            }
        }
        return step_count[end.x][end.y];
    }
};

int main() {
    int n, m;
    int begin_x, begin_y, end_x, end_y;
    cin >> n >> m;
    cin >> begin_x >> begin_y >>  end_x >> end_y;
    Field field(n, m);
    int answer = field.find_step_count(begin_x, begin_y, end_x, end_y);
    if (answer == INF) {
        cout << "NO";
    } else {
        cout << answer;
    }
}
